﻿Imports System.IO
Imports NewHeaderSystems.Archive.Core

Public Class FileHelper
    Public Shared Function GetAccountsFromCsv(ByVal fileName As String) As List(Of AccountView)
        Dim accountList As New List(Of AccountView)
        Dim sr As StreamReader = File.OpenText(fileName)
        Try
            Dim lineNumber As Integer = 0
            Do While sr.Peek() >= 0
                Try
                    lineNumber += 1
                    Dim line As String() = sr.ReadLine().Split(CChar(","))
                    Dim account As New AccountView
                    With account
                        .AccountID = line(0)
                        .CustodianID = line(1)
                        .AccountName = line(2)
                    End With
                    accountList.Add(account)
                Catch ex As Exception
                    Throw New Exception(String.Format("Line {0} is in an invalid format.", lineNumber), ex)
                End Try
            Loop
        Finally
            sr.Close()
        End Try
        Return accountList
    End Function

    Public Shared Function GetSecuritiesFromCsv(ByVal fileName As String) As List(Of SecurityView)
        Dim accountList As New List(Of SecurityView)
        Dim sr As StreamReader = File.OpenText(fileName)
        Try
            Dim lineNumber As Integer = 0
            Do While sr.Peek() >= 0
                Try
                    lineNumber += 1
                    Dim line As String() = sr.ReadLine().Split(CChar(","))
                    Dim account As New SecurityView
                    With account
                        .Ticker = line(0)
                        .Cusip = line(1)
                        .Description = line(2)
                    End With
                    accountList.Add(account)
                Catch ex As Exception
                    Throw New Exception(String.Format("Line {0} is in an invalid format.", lineNumber), ex)
                End Try
            Loop
        Finally
            sr.Close()
        End Try
        Return accountList
    End Function
End Class
